package com.yuncheng.spcyApi.flowController;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.CommonIdsBo;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxJob;
import com.yuncheng.spcyApi.entity.SpcyZltxwjFlow;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.entity.SpcyZltxwjXgpxwj;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.flow.TxwjFlowService;
import com.yuncheng.spcyApi.mapper.SpcyZltxwjFlowMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyZltxwjFlowService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.LocalFileUploadUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.flow.CommonFlowVo;
import com.yuncheng.spcyApi.vo.flow.ZltxwjFlowListVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyReturnVo;
import com.yuncheng.spcyApi.vo.spcy.ZltxwjXgwjVo;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "审评查验-质量体系文件流程接口")
@RestController
@RequestMapping("/api/spzx-spcy/flow-zltxwj")
public class ZltxwjFlowController {
    private static final Logger log = LoggerFactory.getLogger(ZltxwjFlowController.class);

    @Resource
    private ISpcyZltxwjFlowService spcyZltxwjFlowService;

    @Resource
    @Lazy
    private TxwjFlowService txwjFlowService;

    @Resource
    private SpcyZltxwjFlowMapper spcyZltxwjFlowMapper;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private ISpcyLogService spcyLogService;

    @Log(title = "流程监控列表-分页查询")
    @ApiOperation(value = "流程监控列表-分页查询")
    @GetMapping("zltxwjFlowJkPage")
    public HttpResult<?> zltxwjFlowJkPage(ZltxwjFlowListVo flowVo, PageVo pageVo, OrderItemVo orderItemVo,
                                          @RequestParam(name = "onlyCurrentUser", defaultValue = "false") boolean onlyCurrentUser){

            flowVo.setFblzt(SpcyConstant.BLZT_DB);
            flowVo.setFlcmc(SpcyFlowConstant.ZLTXWJ_LCMC);
            Page<ZltxwjFlowListVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
            String userid = null;
            if (onlyCurrentUser){
                GetCurrentUserUtils.getCurrentUserId();
            }
            IPage<ZltxwjFlowListVo> list = spcyZltxwjFlowMapper.selectFlowJobPage(page, userid, flowVo, orderItemVo);

            return HttpResult.ok(list);
    }

    @Log(title = "流程列表-分页查询")
    @ApiOperation(value = "流程列表-分页查询")
    @GetMapping("zltxwjFlowJobPage")
    public HttpResult<?> flowJobPage(ZltxwjFlowListVo flowVo, PageVo pageVo, OrderItemVo orderItemVo,
                                     @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser){

            flowVo.setFblzt(SpcyConstant.BLZT_DB);
            flowVo.setFlcmc(SpcyFlowConstant.ZLTXWJ_LCMC);
            Page<ZltxwjFlowListVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
            String userid = null;
            if (onlyCurrentUser){
                userid = GetCurrentUserUtils.getCurrentUserId();
            }
            IPage<ZltxwjFlowListVo> list = spcyZltxwjFlowMapper.selectFlowJobPage(page, userid, flowVo, orderItemVo);

            return HttpResult.ok(list);

    }

    @Log(title = "流程列表-分页查询-我的已办")
    @ApiOperation(value = "流程列表-分页查询-我的已办")
    @GetMapping("myDoneZltxwjFlow")
    public HttpResult<?> myDoneZltxwjFlow(ZltxwjFlowListVo flowVo, PageVo pageVo, OrderItemVo orderItemVo,
                                          @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser){

            flowVo.setFblzt(SpcyConstant.BLZT_YB);
            flowVo.setFlcmc(SpcyFlowConstant.ZLTXWJ_LCMC);
            Page<ZltxwjFlowListVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
            String userid = GetCurrentUserUtils.getCurrentUserId();
            IPage<ZltxwjFlowListVo> list = spcyZltxwjFlowMapper.selectYbFlowJobPage(page, userid, flowVo, orderItemVo);

            /*if (CollectionUtil.isNotEmpty(list.getRecords())){
                list.setRecords(list.getRecords().stream().collect(Collectors.collectingAndThen(
                        Collectors.toCollection(
                                () -> new TreeSet<>(
                                        Comparator.comparing(o -> o.getZltxwjid())
                                )
                        ), ArrayList<ZltxwjFlowListVo>::new
                )));
            }*/

            return HttpResult.ok(list);

    }

    @Log(title = "查询所有-我的质量体系文件流程列表")
    @ApiOperation(value = "查询所有-我的质量体系文件流程列表")
    @GetMapping(value = "queryAllMyXdZltxwjFlowList")
    public HttpResult<?> queryAllMyXdZltxwjFlowList(SpcyZltxwjFlow flow, PageVo pageVo,
                                                  @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser){
        CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
        if (user == null){
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<SpcyZltxwjFlow> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<SpcyZltxwjFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(flow.getFwjmc()),SpcyZltxwjFlow::getFwjmc,flow.getFwjmc());
        queryWrapper.like(StringUtils.isNotBlank(flow.getFxglx()),SpcyZltxwjFlow::getFxglx,flow.getFxglx());
        queryWrapper.like(StringUtils.isNotBlank(flow.getFwjbh()),SpcyZltxwjFlow::getFwjbh,flow.getFwjbh());
        queryWrapper.like(StringUtils.isNotBlank(flow.getFtype()),SpcyZltxwjFlow::getFtype,flow.getFtype());
        queryWrapper.like(StringUtils.isNotBlank(flow.getFsqr()),SpcyZltxwjFlow::getFsqr,flow.getFsqr());
        queryWrapper.like(StringUtils.isNotBlank(flow.getFsqks()),SpcyZltxwjFlow::getFsqks,flow.getFsqks());
        queryWrapper.like(StringUtils.isNotBlank(flow.getFzt()),SpcyZltxwjFlow::getFzt,flow.getFzt());

        if (onlyCurrentUser){
            queryWrapper.like(StringUtils.isNotBlank(flow.getFsqrid()),SpcyZltxwjFlow::getFsqrid,user.getId());
        }

        queryWrapper.orderByDesc(SpcyZltxwjFlow::getCreateTime);
        IPage<SpcyZltxwjFlow> flowList = spcyZltxwjFlowService.page(page, queryWrapper);
        return HttpResult.ok(flowList);
    }

    @Log(title = "发起修订-质量体系文件流程")
    @ApiOperation(value = "发起修订-质量体系文件流程")
    @ApiModelProperty(name = "id",value = "质量体系文件id")
    @GetMapping(value = "initZltxwjFlow")
    public HttpResult<?> initZltxwjFlow(@RequestParam(value = "id",defaultValue = "")String id){

            Result result = txwjFlowService.initZltxwjFlow(id, false);
            if (!result.isSuccess()) {
                return HttpResult.error(result.getMessage());
            } else {
                HttpResult result1 = new HttpResult(true);
                result1.setResult(result.getResult());
                result1.setMessage("发起修订文件成功");
                return result1;
            }
    }

    @Log(title = "发起失效-质量体系文件流程")
    @ApiOperation(value = "发起失效-质量体系文件流程")
    @ApiModelProperty(name = "id",value = "质量体系文件id")
    @GetMapping(value = "initZltxwjSxFlow")
    public HttpResult<?> initZltxwjSxFlow(@RequestParam(value = "id",defaultValue = "")String id){

            Result result = txwjFlowService.initZltxwjFlow(id, true);
            if (!result.isSuccess()) {
                return HttpResult.error(result.getMessage());
            } else {
                HttpResult result1 = new HttpResult(true);
                result1.setResult(result.getResult());
                result1.setMessage("发起失效文件成功");
                return result1;
            }
    }

    @Log(title = "发起添加-质量体系文件流程")
    @ApiOperation(value = "发起添加-质量体系文件流程")
    @GetMapping(value = "addInitZltxwjFlow")
    public HttpResult<?> addInitZltxwjFlow(){

            Result result = txwjFlowService.addInitZltxwjFlow();
            if (!result.isSuccess()) {
                return HttpResult.error(result.getMessage());
            } else {
                HttpResult result1 = new HttpResult(true);
                result1.setResult(result.getResult());
                result1.setMessage("发起新增文件成功");
                return result1;
            }
    }

    @Log(title = "发起宣贯培训-质量体系文件流程")
    @ApiOperation(value = "发起宣贯培训-质量体系文件流程")
    @PostMapping(value = "xgpxInitZltxwjFlow")
    public HttpResult xgpxInitZltxwjFlow(@RequestBody CommonIdsBo reqVo){

        Result result = txwjFlowService.xgpxInitZltxwjFlow(reqVo.getIds());
        if (!result.isSuccess()) {
            return HttpResult.error(result.getMessage());
        } else {
            HttpResult result1 = new HttpResult(true);
            result1.setResult(result.getResult());
            result1.setMessage("发起宣贯培训成功");
            return result1;
        }
    }

    @Log(title = "查询当前选中的体系文件列表")
    @ApiOperation(value = "查询当前选中的体系文件列表")
    @PostMapping(value = "cxChoiceTxwjList")
    public HttpResult cxChoiceTxwjList(@RequestBody CommonIdsBo reqVo){

        return txwjFlowService.selectNowSztxwjList(reqVo.getIds());
    }

    @Log(title = "将修订文件-移送到资源库")
    @ApiOperation(value = "将修订文件-移送到资源库")
    @GetMapping(value = "ysFlowToZltxwjBasic")
    public HttpResult<?> ysFlowToZltxwjBasic(@RequestParam(value = "zltxwjid",defaultValue = "") String id){

        Result result = txwjFlowService.ysFlowToZltxwjBasic(id);
        if (!result.isSuccess()) {
            return HttpResult.error(result.getMessage());
        }


        return HttpResult.ok("移送入库成功");
    }

    @Log(title = "质量体系文件-流程删除")
    @ApiOperation(value = "质量体系文件-流程删除")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "zltxwjid",value = "质量体系文件id,多个参数逗号分割")
    })
    @GetMapping(value = "deleteZltxwjFlow")
    public HttpResult<?> deleteZltxwjFlow(@RequestParam(value = "zltxwjid",defaultValue = "")String zltxwjid){
        if (StringUtils.isBlank(zltxwjid)){
            throw new BusinessException("质量体系文件参数丢失，请联系管理员");
        }

            List<String> jobids = StringPatternUtils.getListByIds(zltxwjid);
            for (String id : jobids) {
                Result result = txwjFlowService.deleteZltxwjFlow(id);
                if (!result.isSuccess()) {
                    return HttpResult.error(result.getMessage());
                }
            }


        return HttpResult.ok("删除成功");
    }

    @Log(title = "质量体系文件流程保存接口")
    @ApiOperation(value = "质量体系文件流程保存接口")
    @PostMapping(value = "/saveZltxwjFlow")
    public HttpResult<?> saveZltxwjFlow(@RequestBody CommonFlowVo flowVo) {

        txwjFlowService.handleFlow(flowVo,flowVo.getJobid(),null,SpcyFlowConstant.SUBMIT_TYPE_SAVE,flowVo.getOpinion());

        return HttpResult.ok("保存成功");
    }

    @Log(title = "质量体系文件流程提交接口")
    @ApiOperation(value = "质量体系文件流程提交接口")
    @PostMapping(value = "/submitZltxwjFlow")
    public HttpResult<?> submitZltxwjFlow(@RequestBody CommonFlowVo flowVo) {

        txwjFlowService.handleFlow(flowVo,flowVo.getJobid(),null,SpcyFlowConstant.SUBMIT_TYPE_SUBMIT,flowVo.getOpinion());


        return HttpResult.ok("提交成功");
    }

    @Log(title = "撤回流程")
    @ApiOperation(value = "撤回流程")
    @GetMapping(value = "returnZltxwjFlow")
    public HttpResult<?> returnSpBzFlow(@RequestParam(value = "jobid",defaultValue = "")String jobid,
                                        @RequestParam(value = "opinion",defaultValue = "")String opinion){

            SpcyReturnVo returnVo = new SpcyReturnVo();
            returnVo.setJobid(jobid);
            returnVo.setOpinion(opinion);

            txwjFlowService.getReturnUpperFlowJob(returnVo);

        return HttpResult.ok("撤回成功");
    }

    @Log(title = "查询当前流程任务详情-通过当前流程任务id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiImplicitParam(name = "jobid",value = "流程任务id",required = true)
    @GetMapping(value = "queryJobByJobId")
    public HttpResult<?> queryJobByJobId(@RequestParam(value = "jobid",defaultValue = "")String jobid){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("流程任务id不能为空");
        }


        Map map = txwjFlowService.getJobConcernByJobId(jobid);
        return HttpResult.ok(map);
    }

    @Log(title = "质量体系文件-历史流转记录")
    @ApiOperation(value = "质量体系文件-历史流转记录")
    @ApiImplicitParam(name = "zltxwjid",value = "质量体系文件id")
    @GetMapping(value = "queryJobHistoryList")
    public HttpResult<?> queryJobHistoryList(@RequestParam(value = "zltxwjid",defaultValue = "")String zltxwjid){

        if (StringUtils.isBlank(zltxwjid)){
            return HttpResult.error("质量体系文件参数不能为空");
        }

        List<SpcySqsxJob> jobListBySqsxId = txwjFlowService.getJobListBySqsxId(zltxwjid);

        Map resultMap = new HashMap();
        resultMap.put("records",jobListBySqsxId);
        resultMap.put("total",jobListBySqsxId.size());
        return HttpResult.ok(resultMap);

    }

    @Log(title = "批量打印流转审批表")
    @ApiOperation(value = "批量打印流转审批表")
    @ApiImplicitParam(name = "ids",value = "质量体系文件zltxwjid,多个英文逗号拼接")
    @PostMapping(value = "queryPrintZltxwjTable")
    public HttpResult<?> queryPrintZltxwjTable(@RequestBody HashMap<String,String> map){

            return txwjFlowService.queryPrintZltxwjTable(map);
    }

    @Log(title = "查询宣贯学习人员目录列表")
    @ApiOperation(value = "查询宣贯学习人员目录列表")
    @GetMapping(value = "cxXgryMl")
    public HttpResult cxXgryMl(@RequestParam(value = "zltxwjid",defaultValue = "") String zltxwjid){
        if (StringUtils.isBlank(zltxwjid)){
            return HttpResult.error("体系参数为空！");
        }

        // 宣贯学习人员
        return HttpResult.ok(txwjFlowService.selectXgxxRymlList(spcyZltxwjFlowMapper.selectFxgxxryidById(zltxwjid)));
    }

    @Log(title = "查询宣贯学习人员姓名列表")
    @ApiOperation(value = "查询宣贯学习人员姓名列表")
    @GetMapping(value = "queryXgxxRyxmList")
    public HttpResult queryXgxxRyxmList(@RequestParam(value = "zltxwjid",defaultValue = "") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        // 宣贯学习人员
        String fxgxxryid = spcyZltxwjFlowMapper.selectFxgxxryidById(id);

        return HttpResult.ok(txwjFlowService.getXgxxRyxmList(fxgxxryid));
    }

    @Log(title = "查询宣贯学习人员信息列表")
    @ApiOperation(value = "查询宣贯学习人员信息列表")
    @GetMapping(value = "queryXgxxryList")
    public HttpResult queryXgxxryList(@RequestParam(value = "zltxwjid",defaultValue = "") String id){

        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        List<SpcySqsxJob> list = spcyZltxwjFlowMapper.selectXgxxJobList(id, SpcyFlowConstant.ZLTXWJ_HJMC_XGXX);
        return HttpResult.ok(list);
    }

    @Log(title = "流程质量体系文件-上传文件")
    @ApiOperation(value = "流程质量体系文件-上传文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file",value = "文件不能为空",required = true),
            @ApiImplicitParam(name = "zltxwjid",value = "质量体系文件id",required = true)
    })
    @PostMapping(value = "/uploadZltxwjFile")
    public HttpResult<?> uploadZltxwjFile(@RequestPart("file") MultipartFile file, @RequestParam(value = "zltxwjid",defaultValue = "") String id){

        if (StringUtils.isEmpty(id)){
            return HttpResult.error("质量体系文件参数不能为空");
        }

        SpcyZltxwjFlow zltxwj = spcyZltxwjFlowService.getById(id);
        if (zltxwj == null){
            return HttpResult.error("当前质量体系文件记录不存在，请刷新重试");
        }

        /**
         * 如果旧的文件存在，将旧的文件移动到新的文件目录
         */
        String oldCflj = zltxwj.getFwjlj(); // 旧的文件路径
        String oldWjlj = zltxwj.getFjdwjlj(); // 旧的文件路径
        if (StringUtils.isNotBlank(oldWjlj)){
            // 存储旧的质量体系文件库
            localFileUploadUtils.deleteLocalFileUrlByZltxwj(oldWjlj); // 删除文件
        }
        if (StringUtils.isNotBlank(oldCflj)){
            localFileUploadUtils.deleteLocalFileUrlByZltxwj(oldCflj); // 删除文件
        }

        String originalFilename = file.getOriginalFilename();
        // 文件类型
        String wjlx = originalFilename.substring(originalFilename.lastIndexOf("."));

        String wjccmc = GetUuIdUtils.ReplaceUuId() + wjlx;
        String nowDay = DateConversionUtils.DateToMonthTwo(new Date()); // 获取当前日期 yyyy-MM-dd
        String ml = SpcyConstant.Flow_Zltxwj_File_Base_Url + "/" + nowDay;

        localFileUploadUtils.uploadZltxwjFile(file, wjccmc, ml); // 上传文件

        zltxwj.setFwjlx(wjlx);
        String cflj = "/" + ml + "/" + wjccmc;
        zltxwj.setFwjlj(cflj);
        zltxwj.setFscrq(new Date());
        boolean flag = spcyZltxwjFlowService.updateById(zltxwj);
        if (flag) {

            String czContent = "上传质量体系文件: ";
            Map<String, Object> wjjlMap = ObjectToMapUtils.objectToMap(zltxwj);
            czContent += LogUtils.MapToStringMethod(wjjlMap);
            spcyLogService.addLogs(LogConstant.LOGS_ZLTXWJ, czContent, id, SpcyConstant.LOGS_SYS_TYPE_BC, SpcyConstant.LOGS_SYS_TYPE_BC);
        }

        return HttpResult.ok("上传成功");
    }

    @Log(title = "流程质量体系文件-删除文件")
    @ApiOperation(value = "流程质量体系文件-删除文件")
    @DeleteMapping(value = "/deleteZltxwjFile")
    public HttpResult deleteZltxwjFile(@RequestParam(value = "zltxwjid",defaultValue = "") String fileId) {
        if (StringUtils.isEmpty(fileId)){
            return HttpResult.error("质量体系文件参数为空");
        }

        SpcyZltxwjFlow zltxwj = spcyZltxwjFlowService.getById(fileId);
        if (zltxwj == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        if (StringUtils.isNotBlank(zltxwj.getFwjlj())) {
            localFileUploadUtils.deleteLocalFileUrlByZltxwj(zltxwj.getFwjlj());
        }

        if (StringUtils.isNotBlank(zltxwj.getFjdwjlj())) {
            localFileUploadUtils.deleteLocalFileUrlByZltxwj(zltxwj.getFjdwjlj());
        }
        zltxwj.setFwjlj("");
        zltxwj.setFjdwjlj("");
        zltxwj.setFwjlx("");
        spcyZltxwjFlowService.updateById(zltxwj);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "流程质量体系文件-下载文件")
    @ApiOperation(value = "流程质量体系文件-下载文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "zltxwjid",value = "质量体系文件id",required = true)
    })
    @GetMapping(value = "/getZltxwjFile")
    public void getZltxwjFile(@RequestParam(value = "zltxwjid",defaultValue = "") String fileId, HttpServletResponse response) {
        if (StringUtils.isEmpty(fileId)){
            log.error("质量体系文件id为空");
            return;
        }
        SpcyZltxwjFlow zltxwj = spcyZltxwjFlowService.getById(fileId);
        if (zltxwj == null){
            log.error("质量体系文件id为-{}的文件记录不存在",fileId);
            return;
        }

        localFileUploadUtils.ejzLocalFileURlByZltxwj(zltxwj.getFwjlj(),response);
    }

    @ApiOperation(value = "流程质量体系文件-下载文件-跨域请求")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "zltxwjid",value = "质量体系文件id",required = true)
    })
    @GetMapping(value = "/getFileCross")
    public void getFileCross(@RequestParam(value = "zltxwjid",defaultValue = "") String fileId, HttpServletResponse response) {
        if (StringUtils.isEmpty(fileId)){
            throw new BusinessException("质量体系文件参数为空");
        }

        SpcyZltxwjFlow zltxwj = spcyZltxwjFlowService.getById(fileId);
        if (zltxwj == null){
            log.error("质量体系文件文件记录不存在{}",fileId);
            throw new BusinessException("没有相匹配的数据！");
        }
        if (StringUtils.isBlank(zltxwj.getFwjlj())){
            log.error("体系文件为空{}", fileId);
            throw new BusinessException("体系文件为空！");
        }

        localFileUploadUtils.ejzLocalFileURlByZltxwj(zltxwj.getFwjlj(), response);
    }

    @Log(title = "上传文件-宣贯学习文件")
    @ApiOperation(value = "上传文件-宣贯学习文件")
    @PostMapping(value = "uploadXgxxFile")
    public HttpResult uploadXgxxFile(@RequestParam("file")MultipartFile[] file,
                                     @RequestParam(value = "zltxwjid",defaultValue = "")String zltxwjid){


            Result result = txwjFlowService.uploadXgxxFile(file, zltxwjid);
            if (!result.isSuccess()) {
                return HttpResult.error(result.getMessage());
            }

        return HttpResult.ok("上传成功");
    }

    @Log(title = "下载文件-宣贯学习文件")
    @ApiOperation(value = "下载文件-宣贯学习文件")
    @ApiImplicitParam(name = "id",value = "文件id")
    @GetMapping(value = "downloadXgxxFile")
    public void downloadXgxxFile(@RequestParam(value = "id",defaultValue = "")String id,HttpServletResponse response){
        txwjFlowService.downloadXgxxFile(id, response);
    }

    @Log(title = "删除文件-宣贯学习文件")
    @ApiOperation(value = "删除文件-宣贯学习文件")
    @ApiImplicitParam(name = "id",value = "文件id")
    @DeleteMapping(value = "deleteXgxxFile")
    public HttpResult deleteXgxxFile(@RequestParam(value = "id",defaultValue = "")String id){

        Result result = txwjFlowService.deleteXgxxFile(id);
        if (!result.isSuccess()) {
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "查询文件-宣贯学习文件")
    @ApiOperation(value = "查询文件-宣贯学习文件-附件列表")
    @GetMapping(value = "queryXgxxFile")
    public HttpResult queryXgxxFile(@RequestParam(value = "zltxwjid",defaultValue = "ASFAF")String zltxwjid){

        return txwjFlowService.queryXgxxFile(zltxwjid);
    }

//    @ApiOperation(value = "发布-宣贯学习-环节流程")
    @GetMapping(value = "fbxgxxJob")
    public HttpResult fbxgxxJob(@RequestParam(value = "zltxwjid",defaultValue = "")String zltxwjid,
                                @RequestParam(value = "jobid",defaultValue = "") String jobid){
        Result result = txwjFlowService.releaseXgxxJob(zltxwjid, null, jobid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok();
    }

    @Log(title = "查询当前选择的宣贯培训-文件列表-根据流程id")
    @ApiOperation(value = "查询当前选择的宣贯培训-文件列表-根据流程id")
    @GetMapping(value = "queryXgpxFjList")
    public HttpResult queryXgpxFjList(@RequestParam(value = "jobid",defaultValue = "")String jobid){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("流程参数为空！");
        }

        SpcySqsxJob job = spcyZltxwjFlowMapper.selectJobJbxxById(jobid);
        if (job == null){
            return HttpResult.error("流程任务数据不存在！");
        }
        String fuserid = null;
        if (job.getFhjmc().equals(SpcyFlowConstant.ZLTXWJ_HJMC_XGXX)){
            fuserid = job.getFdbrid();
        }
        List<ZltxwjXgwjVo> list = txwjFlowService.selectXgpxwjInfo(job.getFpid(), fuserid);

        return HttpResultVo.HttpResultList(list);
    }

    @Log(title = "查询宣贯培训文件详情列表-根据对应人员进行查询")
    @ApiOperation(value = "查询宣贯培训文件详情列表-根据对应人员进行查询")
    @GetMapping(value = "queryRyXgpxFjList")
    public HttpResult queryRyXgpxFjList(@RequestParam(value = "zltxwjid",defaultValue = "")String zltxwjid){
        if (StringUtils.isBlank(zltxwjid)){
            return HttpResult.error("体系参数为空！");
        }

        String fxgxxryid = spcyZltxwjFlowMapper.selectFxgxxryidById(zltxwjid);
        List<SpcySqsxJob> jobList = spcyZltxwjFlowMapper.selectXgxxJobList(zltxwjid, SpcyFlowConstant.ZLTXWJ_HJMC_XGXX);
        if (StringUtils.isBlank(fxgxxryid)){

            return HttpResultVo.HttpResultList(txwjFlowService.selectXgpxwjInfo(zltxwjid, null));
        }else {
            List<ZltxwjXgwjVo> dataList = new ArrayList<>();
            List<YcSysUser> userList = txwjFlowService.getXgxxRyxmList(fxgxxryid);
            for (YcSysUser user : userList) {
                List<SpcySqsxJob> userJobList = jobList.stream()
                        .filter(f -> f.getFdbrid().equals(user.getId()))
                        .collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(userJobList)) {
                    dataList.addAll(txwjFlowService.selectXgpxwjInfo(zltxwjid, user.getId(), user.getRealname(), userJobList.get(0)));
                }else {
                    dataList.addAll(txwjFlowService.selectXgpxwjInfo(zltxwjid, user.getId(), user.getRealname(), null));
                }
            }

            return HttpResultVo.HttpResultList(dataList);
        }
    }

    @Log(title = "更新宣贯培训文件-学习意见")
    @ApiOperation(value = "更新宣贯培训文件-学习意见")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "宣贯培训文件id"),
            @ApiImplicitParam(name = "fxxyj",value = "学习意见")
    })
    @PostMapping(value = "updateXgwjXxyjById")
    public HttpResult updateXgwjXxyjById(@RequestBody SpcyZltxwjXgpxwj bean){

        return txwjFlowService.updateXgwjXxyjById(bean);
    }

    /*@Log(title = "勾选新增宣贯培训文件记录")
    @ApiOperation(value = "勾选新增宣贯培训文件记录")*/
    @PostMapping(value = "selectAddXgpxwj")
    public HttpResult selectAddXgpxwj(@RequestParam(value = "zltxwjid") @ApiParam("体系文件id") String zltxwjid,
                                      @RequestParam(value = "ids") @ApiParam("选择体系文件库ids") String ids){

        return txwjFlowService.selectAddXgpxwj(zltxwjid, ids);
    }

    /*@Log(title = "删除宣贯培训文件记录-根据id")
    @ApiOperation(value = "删除宣贯培训文件记录-根据id")*/
    @DeleteMapping(value = "delXgpxwj")
    public HttpResult delXgpxwj(@RequestParam(value = "id",defaultValue = "") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        Result result = txwjFlowService.deleteXgpxwjList(id, null, null);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("删除成功");
    }



}
